package edu.android.com;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.FrameLayout;

public class node extends View {

	private float mRay;
	private int mNodeNumber;
	private int mColor,mTextColor;
	private float textSize;
	private float mX,mY;
	private boolean mUsed;
	private String text;
	public ArrayList <edge> mEdges = new ArrayList();
	
	public node(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		
		initialize();
	}
	
	public void initialize() {
		// TODO Auto-generated method stub
		mColor = Color.LTGRAY;
		mTextColor = Color.DKGRAY;
		mRay = 25 * ((GraphTheory.heightScale + GraphTheory.widthScale)/2f);
		textSize = 25 * ((GraphTheory.heightScale + GraphTheory.widthScale)/2f);
		mUsed = false;
		text = mNodeNumber + "";
		
		invalidate();
	}

	@Override
	public void onDraw(Canvas canvas){
		//canvas.drawColor(Color.WHITE);
		
		Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		Paint mPaintText = new Paint(Paint.ANTI_ALIAS_FLAG);
		
		mPaint.setColor(mColor);
		mPaintText.setColor(mTextColor);
		mPaintText.setStyle(Style.FILL);
		mPaintText.setTextSize(textSize);
		
		if(edge.mWithCost)
			mPaintText.setColor(Color.RED);
		
		canvas.drawCircle(mX, mY, mRay, mPaint);
		canvas.drawText(text,mX-mRay/4 ,mY+mRay/4, mPaintText);
		
		//update edges
		updateEdges();
	}
	
	//check and add edge
	public void addEdge(edge mEdge){
		mEdges.add(mEdge);
	}
	public boolean edgeExist(node b){
		boolean t = false;
		int len = mEdges.size();
		for(int i = 0;i < len;i ++){
			if(b == mEdges.get(i).firstNode || b == mEdges.get(i).secondNode){
				t = true;
				break;
			}
		}
		return t;
	}
	
	public void removeEdges(FrameLayout ll){
		for(int i = 0;i < mEdges.size();i ++){
			if(ll.findViewById(mEdges.get(i).getId()) != null)
				ll.removeView(mEdges.get(i));
		}
		mEdges.clear();
	}
	public edge removeEdge(node b){
		edge ans = null;
		int len = mEdges.size();
		for(int i = 0;i < len;i ++){
			if(b == mEdges.get(i).firstNode || b == mEdges.get(i).secondNode){
				ans = mEdges.get(i);
				mEdges.remove(i);
				break;
			}
		}
		return ans;
	}
	
	public void updateEdges(){
		int len = mEdges.size();
		for(int i = 0;i < len;i ++){
			mEdges.get(i).invalidate();
		}
	}
	public void setUsed(int color){
		setColor(color);
		mUsed = true;
	}
	public void setNodeNumber(int number){
		mNodeNumber = number;
		text = mNodeNumber + "";
	}
	public void setXY(float X,float Y){
		mX = X;
		mY = Y;
	}
	public boolean getUsed(){
		return mUsed;
	}
	public int getNodeNumber(){
		return mNodeNumber;
	}
	public void setColor(int color){
		mColor = color;
	}
	public void setTextColor(int color){
		mTextColor = color;
	}
	public void setText(String str){
		text = str;
	}
	public float getX(){
		return mX;
	}
	public float getY(){
		return mY;
	}
	public float getRay(){
		return mRay;
	}
}
